package com.hello.tong.service.impl;

import com.hello.tong.entity.Order;
import com.hello.tong.feign.AccountFeignService;
import com.hello.tong.feign.StorageFeignService;
import com.hello.tong.mapper.OrderMapper;
import com.hello.tong.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tong-exists
 * @since 2022-12-31
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountFeignService accountService;

    @Override
    public Order create(String userId, String commodityCode, int orderCount) {
        int a = 3 / 0;
        log.info("create...");
        int orerMoney = calcOrderMoney( commodityCode,  orderCount);
        accountService.debit(userId, orerMoney);
        Order order = new Order();
        order.setUserId(userId);
        order.setCommodityCode(commodityCode);
        order.setCount(orderCount);
        order.setMoney(orerMoney);
        orderMapper.insert(order);
        return order;
    }

    private int calcOrderMoney(String commodityCode, int orderCount) {
        return orderCount;
    }
}
